package org.liveboardgames.agricola.control.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;

	import mx.collections.ListCollectionView;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import org.liveboardgames.agricola.control.delegates.DropPeepDelegate;
	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.control.events.DropPeepEvent;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.vo.card.Card;
	import org.liveboardgames.agricola.vo.common.Peep;

	public class DropPeepCommand implements ICommand, IResponder
	{
		private var peep:Peep;
		private var card:Card;

		public function execute(event:CairngormEvent):void
		{
			var delegate:DropPeepDelegate = new DropPeepDelegate(this);
			var dropPeepEvent:DropPeepEvent = DropPeepEvent(event);

			peep = dropPeepEvent.peep;
			card = dropPeepEvent.card;

			delegate.dropPeep(dropPeepEvent);
		}

		public function result(event:Object):void
		{
			var model:AgricolaModelLocator = AgricolaModelLocator.getInstance();
//			model.updateModel(ResultEvent(event).result as ListCollectionView);

			if (peep != null)
			{
				model.actionController.player = GameHelper.getPeepOwner(model.game, peep);
				var peepTriggeredActions:ListCollectionView = GameHelper.getPeepTriggeredActions(card);
				model.actionController.setCard(card, peepTriggeredActions);
				model.actionController.executeAllActions();
			}

		}

		public function fault(info:Object):void
		{
			var faultEvent:FaultEvent = FaultEvent(info);
			Alert.show("Could not drop peep", "Error");
		}

	}
}